<template>
  <div class="login-body-content">
    <span class="error">{{errorText}}</span>
    <input type="text"  v-model="username" placeholder="用户名/手机号码"/>
    <input type="password" v-model="password" placeholder="密码"/>
    <div class="pwd">
      <p>
        <span class="no" :class="{yes:isRememberPwd}" @click="rememberPwd">{{bingo}}</span>
        记住密码
      </p>
      <p class="forgetPwd">忘记密码</p>

    </div>
    <button @click='loginHandler'  class="login-btn">登录</button>
    <p class="go_login">没有账号<span>立即注册</span></p>
  </div>
</template>

<script>
  export default {
    name: "LoginPwd",
    data(){
      return{
        username:'',
        password:'',
        errorText:'',
        remember:false,
        isRememberPwd:false,
        bingo:'',
      }
    },
    methods:{
      rememberPwd(){
        this.isRememberPwd = !this.isRememberPwd;
        if(this.isRememberPwd){
          this.bingo = '✓';
          this.remember = true;
        }else{
          this.bingo = '';
          this.remember = false;
        }
      },
      loginHandler(){
        var username = this.username;
        var password = this.password;

        var _this = this;

        this.$axios.request({
          url:'http://127.0.0.1:8010/api/v1/login/',
          method:'POST',
          data:{
            remember:_this.remember,
            username:username,
            password:password,
          },
          headers:{
            'Content-Type':'application/json',
          }
        }).then(function (msg) {
          console.log(msg.data);
          if(msg.data.code === 1){
            // console.log(msg.data.data.access_token)
            _this.$store.commit('saveTokens',{token:msg.data.data.access_token,
              username:msg.data.data.username,
              expires_in:msg.data.data.expires_in});

            var url = _this.$route.query.return_url;

            if(url){
              _this.$router.push({path:url})
            }else{
              _this.$router.push({name:'FreeCourse'})
            }

          }else{
            _this.errorText = msg.data.data
          }
        }).catch(function (msg) {
          console.log(msg.data)
        })
      }
    },
    mounted(){

    },
  }
</script>

<style scoped>
.login-body-content{
    margin: 0 auto;
    width: 350px;
    height: auto;
  }
  .login-body-content>input{
    width: 100%;
    height: 45px;
    border-radius: 4px;
    text-indent: 20px;
    font-size: 14px;
    margin-bottom: 16px;
    outline: none;
    border: 1px solid #d9d9d9
  }
  .login-body-content>input:focus{
    border: 1px solid rgb(255,194,16);
  }
  .pwd{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .pwd>p{
    font-size: 12px;
    display: flex;
    align-items: center;
  }
  .no{
    display: inline-block;
    border: 1px solid #d9d9d9;
    width: 15px;
    height: 15px;
    border-radius: 2px;
    margin-right: 10px;
    text-align: center;
    cursor: pointer;
  }
  .yes{
    color: #fff;
    background-color:#ffc210 ;
    border: 1px solid #ffc210;
  }
  .forgetPwd{
    font-size: 14px!important;
    color: #9b9b9b;
    cursor: pointer;
  }
  .login-btn{
    background-color:#ffc210 ;
    height: 45px;
    width: 100%;
    border: 1px solid #ffc210;
    border-radius: 5px;
    margin-top: 30px;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
  }
  .go_login{
    padding-top: 20px;
    font-size: 14px;
    text-align: center;
    color: #9b9b9b;

  }
  .go_login>span{
    color:#ffc210 ;
    margin-left: 5px;
    cursor: pointer;
  }
  .error{
    color:#fa6240
  }
</style>
